package com.small.xx.ai.spring.chatclient.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/chat")
public class ChatController {

    private final ChatClient chatClient;

    public ChatController(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }

    @GetMapping("/call")
    public String call(
        @RequestParam(value = "query", defaultValue = "你好，很高兴认识你，能简单介绍一下自己吗？") String query) {
        return chatClient.prompt(query).call().content();
    }

    @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> stream(
        @RequestParam(value = "query", defaultValue = "你好，很高兴认识你，能简单介绍一下自己吗？") String query) {
        return chatClient.prompt(query).stream().content();
    }

    @GetMapping("/call/temperature")
    public String callTemperature(@RequestParam(value = "temperature", defaultValue = "0.5") double temperature,
        @RequestParam(value = "query", defaultValue = "你好，请为创造一首以“你好”为主题的诗") String query) {
        return chatClient.prompt(query).options(OpenAiChatOptions.builder().temperature(temperature).build()).call()
            .content();
    }

    @GetMapping("/call/temperature/min")
    public String callTemperatureMin(
        @RequestParam(value = "query", defaultValue = "你好，请为创造一首以“你好”为主题的诗") String query) {
        return chatClient.prompt(query).options(OpenAiChatOptions.builder().temperature(0.0).build()).call().content();
    }

    @GetMapping("/call/temperature/max")
    public String callTemperatureMax(
        @RequestParam(value = "query", defaultValue = "你好，请为创造一首以“你好”为主题的诗") String query) {
        return chatClient.prompt(query).options(OpenAiChatOptions.builder().temperature(0.9).build()).call().content();
    }
}